package org.example.io.protocol.command;

import org.example.io.util.LogUtil;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class CommandUtil {

    public static final Map<String, ConsoleCommand> commandMap = new ConcurrentHashMap<>();

    static {
        LoginCommand loginCommand = new LoginCommand();
        commandMap.put(loginCommand.commandType(), loginCommand);
        LogoutCommand logoutCommand = new LogoutCommand();
        commandMap.put(logoutCommand.commandType(), logoutCommand);
        SendMessageCommand messageCommand = new SendMessageCommand();
        commandMap.put(messageCommand.commandType(), messageCommand);
        CreateGroupCommand groupCommand = new CreateGroupCommand();
        commandMap.put(groupCommand.commandType(), groupCommand);
    }

    public static ConsoleCommand getCommand(String type) {
        return commandMap.get(type);
    }

    public static void printCommand() {
        StringBuffer sb = new StringBuffer("命令列表：");
        Set<String> keySet = commandMap.keySet();
        Iterator<String> iterator = keySet.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            sb.append((i+1) + "、" + iterator.next() + "    ");
            i++;
        }
        LogUtil.info(sb.toString());
    }
}
